package com.jeesite.modules.dq.web;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.collect.MapUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.my.entity.MyRegions;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.dq.entity.DqPlace;
import com.jeesite.modules.dq.service.DqPlaceService;

/**
 * 地区表Controller
 * @author ccz
 * @version 2025-03-24
 */
@Controller
@RequestMapping(value = "${adminPath}/dq/dqPlace")
public class DqPlaceController extends BaseController {

	@Autowired
	private DqPlaceService dqPlaceService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public DqPlace get(String dqId, boolean isNewRecord) {
		return dqPlaceService.get(dqId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("dq:dqPlace:view")
	@RequestMapping(value = {"list", ""})
	public String list(DqPlace dqPlace, Model model) {
		model.addAttribute("dqPlace", dqPlace);
		return "modules/dq/dqPlaceList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("dq:dqPlace:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<DqPlace> listData(DqPlace dqPlace, HttpServletRequest request, HttpServletResponse response) {
		dqPlace.setPage(new Page<>(request, response));
		Page<DqPlace> page = dqPlaceService.findPage(dqPlace);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("dq:dqPlace:view")
	@RequestMapping(value = "form")
	public String form(DqPlace dqPlace, Model model) {
		model.addAttribute("dqPlace", dqPlace);
		return "modules/dq/dqPlaceForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("dq:dqPlace:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated DqPlace dqPlace) {
		dqPlaceService.save(dqPlace);
		return renderResult(Global.TRUE, text("保存地区表成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("dq:dqPlace:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(DqPlace dqPlace) {
		dqPlaceService.delete(dqPlace);
		return renderResult(Global.TRUE, text("删除地区表成功！"));
	}






	@RequiresPermissions("sys:area:view")
	@RequestMapping(value = "treeData")
	@ResponseBody
	public List<Map<String, Object>> treeData(String excludeCode, String parentCode, String isShowCode) {
		List<Map<String, Object>> mapList = ListUtils.newArrayList();
		DqPlace where = new DqPlace();
		System.out.println("---------------");
		System.out.println(parentCode);
		System.out.println("---------------");
		// 设置父级编码
		if (StringUtils.isNotBlank(parentCode)) {
			where.setDqParentId(parentCode);
		}

		List<DqPlace> list = dqPlaceService.findList(where);
		Set<String> loadedIds = new HashSet<>(); // 用于记录已经加载的节点ID

		for (int i = 0; i < list.size(); i++) {
			DqPlace e = list.get(i);

			// 过滤被排除的编码（包括所有子级）
			if (StringUtils.isNotBlank(excludeCode)) {
				if (e.getDqId().equals(excludeCode)) {
					continue;
				}
				if (e.getDqParentId().contains("," + excludeCode + ",")) {
					continue;
				}
			}

			// 检查是否已经加载过该节点
			if (loadedIds.contains(e.getDqId())) {
				continue;
			}

			Map<String, Object> map = MapUtils.newHashMap();
			map.put("id", e.getId());
			map.put("pId", e.getDqParentId());
			map.put("name", StringUtils.getTreeNodeName(isShowCode, e.getDqParentId(), e.getDqName()));
			map.put("isParent", true);
			mapList.add(map);

			// 记录已经加载的节点ID
			loadedIds.add(e.getDqId());
		}
		return mapList;
	}
	
}